/*
 * Operations sql
 * @authors Andres F. Restrepo A.
 *
 * @version 0.1
 * Date : Jul 24, 2011, 11:56 PM
*/

package com.andres.sqlite;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.andres.tool.Conf;

public class SqlServerAdmin {
	private static final String RUTA_DB=Conf.getValue("PATHSQLITE");
	private Connection con;
	public SqlServerAdmin(){
		try {
			Class.forName("org.sqlite.JDBC");
			con = DriverManager.getConnection("jdbc:sqlite:"+RUTA_DB);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	}
	private void closeConnection(){
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public boolean insert(String name, String hostName,String user,String password,String path){
		try{
			Statement cliente = con.createStatement();
			cliente.executeUpdate("insert into info values ('"+name+"','"+hostName+"','"+user+"','"+password+"','"+path+"');");
		}catch(SQLException e){
			System.out.println(e.getMessage());
			return false;
		}finally{
			closeConnection();
		}
		return true;
		
	}
	public boolean delete(String name){
		try{
			Statement cliente = con.createStatement();
			cliente.executeUpdate("delete from info where name='"+name+"';");
		}catch(SQLException e){
			System.out.println(e.getMessage());
			return false;
		}finally{
			closeConnection();
		}
		return true;
	}
	public boolean edit(String name, String hostName,String user,String password,String path){
		try{
			Statement cliente = con.createStatement();
			cliente.executeUpdate("update info set hostname='"+hostName+"', user ='"+user+"', password ='"+password+"', path='"+path+"'" +
					" where name='"+name+"';");
		}catch(SQLException e){
			System.out.println(e.getMessage());
			return false;
		}finally{
			closeConnection();
		}
		return true;
	}
	public ResultSet consult(){
		try{
			Statement cliente = con.createStatement();
			ResultSet res = cliente.executeQuery("select * from info");
			return res;
		}catch(SQLException e){
			System.out.println(e.getMessage());
			return null;
		}finally{
			
		}
		
		
	}
}
